import { LOCALE_ID, NgModule } from '@angular/core';

import { registerLocaleData } from '@angular/common';
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
import zh, { default as ngLang } from '@angular/common/locales/zh';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { CustomHttpInterceptor, OauthInitializerProvider } from '@core/base';
import { zhCN as dateLang } from 'date-fns/locale';
import { NZ_CONFIG, NzConfig } from 'ng-zorro-antd/core/config';
import { NZ_DATE_LOCALE, NZ_I18N, zh_CN as zorroLang } from 'ng-zorro-antd/i18n';
import { AppComponent } from './app.component';
import { CoreModule } from './core/core.module';
import { LayoutModule } from './layout/layout.module';
import { RoutesModule } from './routes/routes.module';
import { ShareModule } from './share/share.module';
import { NgxTinymceModule } from 'ngx-tinymce';
import { OAuthModule, OAuthModuleConfig } from 'angular-oauth2-oidc';
import { environment } from '@env/environment';
import { NgxPermissionsModule } from 'ngx-permissions';

registerLocaleData(zh);

// 语言配置
const LANG = {
	abbr: 'zh',
	ng: ngLang,
	zorro: zorroLang,
	date: dateLang
};
const LANG_PROVIDES = [
	{ provide: LOCALE_ID, useValue: LANG.abbr },
	{ provide: NZ_I18N, useValue: LANG.zorro },
	{ provide: NZ_DATE_LOCALE, useValue: LANG.date },
];

// NG-ZORRO 全局配置项
const ngZorroConfig: NzConfig = {
	pageHeader: { nzGhost: false },
	notification: { nzPlacement: 'bottomRight' },
	modal: { nzMaskClosable: false },
	drawer: { nzMaskClosable: false },
};
const ZORRO_PROVIDES = [{ provide: NZ_CONFIG, useValue: ngZorroConfig }];

// tinymce 配置
const tinymceConfig = {
	baseURL: './assets/tinymce/',
	config: {
		language: 'zh-Hans',
		branding: false,
		promotion: false
	}
}

// OAuthModule配置
const oauthModuleConfig: OAuthModuleConfig = {
	resourceServer: {
		allowedUrls: [environment.api.baseUrl],
		sendAccessToken: true
	}
}

// 应用配置
const INTERCEPTOR_PROVIDES = [
	{ provide: HTTP_INTERCEPTORS, useClass: CustomHttpInterceptor, multi: true },
];

const APPINIT_PROVIDES = [
	OauthInitializerProvider,
];

@NgModule({
	declarations: [
		AppComponent
	],
	imports: [
		FormsModule,
		HttpClientModule,
		BrowserAnimationsModule,
		CoreModule,
		LayoutModule,
		RoutesModule,
		ShareModule,

		NgxTinymceModule.forRoot(tinymceConfig),
		OAuthModule.forRoot(oauthModuleConfig),
		NgxPermissionsModule.forRoot()
	],
	providers: [
		...LANG_PROVIDES, ...INTERCEPTOR_PROVIDES, ...ZORRO_PROVIDES, ...APPINIT_PROVIDES
	],
	bootstrap: [AppComponent]
})
export class AppModule { }
